package controler;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.SanphamModel;
import entity.Sanpham;

/**
 * Servlet implementation class search
 */
@WebServlet("/search")
public class search extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public search() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws SQLException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public int count = 0;
	
	public String chuoitimtongdong(String ten, String madanhmuc,String thuonghieu,String pricemax,String pricemin)
	{
		String strSearch = "";
		String sWhere = "";
		if(ten==null) ten = "";
		sWhere +=" TENSP like '%"+ten+"%'";

		if(madanhmuc!= "" && madanhmuc !=null) sWhere += "AND MADANHMUC ='"+madanhmuc+"'";
		if(thuonghieu!= "" && thuonghieu !=null) sWhere += "AND thuonghieu ='"+thuonghieu+"'";

    	if(pricemax!="" && pricemax !=null) sWhere+="AND GIABAN<'"+pricemax+"'";

    	if(pricemin !="" && pricemin !=null) sWhere +="AND GIABAN>'"+pricemin+"'";

    	strSearch = "SELECT MASP,MADANHMUC,TENSP, MOTA, GIABAN,"
                + "NGAYDANG, DISCOUNT, URLHINH,URLHINH2,URLHINH3 "
                + "FROM   (SELECT ROW_NUMBER() OVER(ORDER BY TENSP) AS rownumber,* FROM SANPHAM)"
                + "as vt_table  WHERE "+ sWhere;
    	return strSearch;
	}
	
	public String chuoitimkiem(String ten, String madanhmuc,String thuonghieu,String pricemax,String pricemin,int begin_number_row,int end_number_row){
		
		String strSearch = "";
		String sWhere = "";
		
		if(ten ==null) ten ="";
		
		sWhere +=" TENSP like N'%"+ten+"%'";

    	if(madanhmuc!= "" && madanhmuc !=null) sWhere += "AND MADANHMUC ='"+madanhmuc+"'";
    	
    	if(thuonghieu!= "" && thuonghieu !=null) sWhere += "AND thuonghieu ='"+thuonghieu+"'";

    	if(pricemax!="" && pricemax !=null) sWhere+="AND GIABAN<'"+pricemax+"'";

    	if(pricemin !="" && pricemin !=null) sWhere +="AND GIABAN>'"+pricemin+"'";
    	strSearch = "SELECT MASP,THUONGHIEU,MADANHMUC,TENSP, MOTA,soluong,soluongdamua, GIABAN,"
                + "NGAYDANG, DISCOUNT, URLHINH,URLHINH2,URLHINH3 "
                + "FROM   (SELECT ROW_NUMBER() OVER(ORDER BY TENSP) AS rownumber,* FROM SANPHAM WHERE "+ sWhere +")"
                + "as vt_table  where rownumber >= " + begin_number_row + " AND rownumber <= " + end_number_row;
		
		return strSearch;
    	
	}
     
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ServletException{
        
        String ten = request.getParameter("name");
        if(ten!= null){
        	ten = new String(ten.getBytes("8859_1"),"UTF8");
        }
        String madanhmuc = request.getParameter("MADANHMUC");
        String thuonghieu = request.getParameter("thuonghieu");
        if(thuonghieu!=null&&thuonghieu!=""){
        	thuonghieu = new String(thuonghieu.getBytes("8859_1"),"UTF8");
        }
        String pricemax = request.getParameter("pricemax");
        String pricemin = request.getParameter("pricemin");
        
        String strTong = chuoitimtongdong(ten,madanhmuc,thuonghieu,pricemax,pricemin);
        
            request.setCharacterEncoding("UTF-8");
            int page = 1;
            int records = 4;

            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }
            List<Sanpham> list = new ArrayList<Sanpham>();
            SanphamModel model = new SanphamModel();

            int start_row = (page * records) - (records - 1);
            int end_row = start_row + records - 1;
            
            String strSearch = chuoitimkiem(ten,madanhmuc,thuonghieu,pricemax,pricemin,start_row,end_row);

            list = model.timsanpham(strSearch);
            int noOfRecords = model.getTotalRows(strTong);
            //int noOfPage = ;
            int noOfPage = ((int) Math.ceil(noOfRecords * 1.0 / records));

            request.setAttribute("list", list);
            request.setAttribute("start", start_row);
            request.setAttribute("end", end_row);
            request.setAttribute("noOfPages", noOfPage);
            request.setAttribute("currentPage", page);
            request.setAttribute("keyword", ten);
            request.setAttribute("MADANHMUC", madanhmuc);
            request.setAttribute("pricemax", pricemax);
            request.setAttribute("pricemin", pricemin);

            RequestDispatcher view = request.getRequestDispatcher("search.jsp");
            view.forward(request, response);
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
